<template>
  <el-dialog
    v-model="state.dialogVisible"
    :append-to-body="true"
    :before-close="close"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    :destroy-on-close="true"
    top="5vh"
    :modal="model"
    :title="state.dialogTitle"
    :width="width"
  >
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form
          :inline="true"
          label-width="80px"
          :model="tableOptions.queryForm"
        >
          <el-form-item label="资源地址">
            <el-input
              v-model="tableOptions.queryForm.resourceUrl"
              placeholder="资源地址"
            />
          </el-form-item>
          <el-form-item label="资源名称">
            <el-input
              v-model="tableOptions.queryForm.resourceName"
              placeholder="资源名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="refresh-left"
              type="warning"
              @click="handleResetQuery"
            >
              重置
            </el-button>
            <el-button icon="Search" type="primary" @click="queryTableData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
      <vab-query-form-left-panel :span="24">
        <el-button icon="circle-plus" type="primary" @click="handleAdd">
          添加
        </el-button>
        <el-button icon="remove" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      ref="tableRef"
      v-loading="tableOptions.loading"
      border
      stripe
      :data="tableOptions.dataList"
      :height="tableOptions.height"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        type="selection"
        width="50"
      />
      <el-table-column label="序号" type="index" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="资源名称"
        prop="resourceName"
        width="210"
      />
      <el-table-column
        align="center"
        label="资源地址"
        prop="resourceUrl"
        width="460"
      />
      <el-table-column align="center" fixed="right" label="操作" width="120">
        <template #default="{ row }">
          <el-button
            icon="Remove"
            size="small"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="tableOptions.pagination.current"
      :layout="tableOptions.pagination.layout"
      :page-size="tableOptions.pagination.size"
      :total="tableOptions.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <AvaliableResource
      ref="avaliableResourceRef"
      width="60%"
      @confirm="queryTableData"
    />
  </el-dialog>
</template>
<script setup name="RoleResource">
import { useMessage, useMessageBox } from "@/hooks/message";
import { dateFormat } from "@/utils/common";
import { queryTable, deleteObjs } from "@/api/admin/org/roleresource/";
import AvaliableResource from "./roleAvaliableResource.vue";
import { useTable } from "@/hooks/table";

const props = defineProps({
  title: {
    type: String,
    default: "资源权限"
  },
  width: {
    type: String,
    default: "60%"
  },
  model: {
    type: Boolean,
    default: true
  }
});

const tableRef = ref(null);
const avaliableResourceRef = ref(null);

const tableOptions = reactive({
  autoLoad: false,
  queryForm: {
    roleId: "",
    resourceUrl: "",
    resourceName: ""
  },
  height: 360,
  queryFunction: queryTable
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleReset,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange
} = useTable(tableOptions, tableRef);

const state = reactive({
  dialogTitle: props.title,
  dialogVisible: false,
  roleId: ""
});

const handleAdd = () => {
  avaliableResourceRef.value.show(state.roleId);
};

const handleDelete = row => {
  let ids = null;
  if (row && row.id) {
    ids = row.id;
  } else {
    if (tableOptions.dataSelections.length > 0) {
      ids = tableOptions.dataSelections.map(item => item.id).join();
    } else {
      useMessage().warning("未选中任何行");
      return;
    }
  }
  useMessageBox()
    .confirm("删除的岗位将无法恢复，是否继续")
    .then(async () => {
      const { code, msg } = await deleteObjs(ids);
      if (code === 200) {
        useMessage().success(msg);
        await queryTableData();
      } else {
        useMessage().warning(msg);
      }
    });
};

const show = roleId => {
  if (roleId) {
    state.roleId = roleId;
    queryTableData({ roleId: roleId }, true, true);
    state.dialogVisible = true;
  }
};

const close = () => {
  handleReset();
  state.dialogVisible = false;
};

defineExpose({
  show,
  close
});
</script>
<style lang="scss" scoped></style>
